package com.isczy.oss.controller;

import com.isczy.oss.service.OssService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/********************************************
 * @Brief: oss上传
 * @Version: 1.0
 * @Date: 2020年12月30日 10:18:18
 * @Author: ChangZiYang
 * ******************************************
 */
@Slf4j
@Api(description = "oss上传控制器")
@RestController
@RequestMapping("eduoss/fileoss")
public class OssController {

    @Autowired
    private OssService ossService;

    /***
     * ***************************************
     * 功能描述: 上传文件到oss
     * @param file
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2020-12-30 10:22】
     * @author modify:
     */
    @ApiOperation(value = "上传文件到oss")
    @PostMapping("uploadOssFile")
    public DataResult uploadOssFile(MultipartFile file){
        try {
            return DataResult.builder()
                    .data(ossService.uploadOssFile(file))
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("上传文件到oss失败")
                    .build();
        }
    }
}
